package com.cn.ZhiKeDianShang.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.ui.fragment.OrderListFragment;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.util.WebMoreDialognew;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class OrderListActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {

    @BindView(R.id.setting_back)
    ImageView settingBack;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.im_sousuo)
    ImageView imSousuo;
    @BindView(R.id.im_choose)
    ImageView imChoose;
    @BindView(R.id.rl_appbarlayout)
    LinearLayout rlAppbarlayout;
    @BindView(R.id.tl_tabs)
    TabLayout tlTabs;
    @BindView(R.id.vp_conViewPagertent)
    ViewPager vpConViewPagertent;

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_user_orderlist;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_user_orderlist;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
        ArrayList<String> titleDatas = new ArrayList<>();
        titleDatas.add("今日任务");
        titleDatas.add("历史任务");
        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.add(OrderListFragment.getInstance(0));
        fragmentList.add(OrderListFragment.getInstance(1));
        MyViewPageAdapter myViewPageAdapter = new MyViewPageAdapter(getSupportFragmentManager(), titleDatas, fragmentList);
        vpConViewPagertent.setAdapter(myViewPageAdapter);
        tlTabs.setupWithViewPager(vpConViewPagertent);
        tlTabs.setTabsFromPagerAdapter(myViewPageAdapter);

//        //下面形式调用
//        tlTabs.post(new Runnable() {
//            @Override
//            public void run() {
//                setIndicator(tlTabs, 50, 50);
//            }
//        });
    }


    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.setting_back, R.id.im_sousuo, R.id.im_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setting_back:
                finish();
                break;
            case R.id.im_sousuo:
                if (etText.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入任务编号");
                    return;
                }
                Intent intent = new Intent(OrderListActivity.this, SerchlistActivity.class);
                intent.putExtra("type", 0);
                intent.putExtra("tasknum", etText.getText().toString());
                startActivity(intent);
                break;
            case R.id.im_choose:
                Log.d("Debug", "点击弹出侧滑框");
                WebMoreDialognew webMoreDialognew = new WebMoreDialognew(OrderListActivity.this, new WebMoreDialognew.BrowserActionClickListener() {
                    @Override
                    public void floatingWindow() {

                    }
                });
                webMoreDialognew.show();
                break;
        }
    }


    public class MyViewPageAdapter extends FragmentPagerAdapter {
        private ArrayList<String> titleList;
        private ArrayList<Fragment> fragmentList;

        public MyViewPageAdapter(FragmentManager fm, ArrayList<String> titleList, ArrayList<Fragment> fragmentList) {
            super(fm);
            this.titleList = titleList;
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }
    }


    private void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }


}
